package net.apps123.soundmeter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;

import net.apps123.fragment.FuPinFragment;
import net.apps123.view.HunXiangAdapter;
import net.apps123.view.SplDataBean;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;


public class FuPinResultActivity extends Activity {

//      100、125、160、200、250、315、400、500、630、800、1000、1250、2000、2500、3150、4000、5000。

    private HashMap<String, List<Integer>> hunXiangMap = new HashMap<String, List<Integer>>();
    private Context mContext;
    private LineChart lineChart;

    private HashMap<String, short[]> deviceList = new HashMap<>();

    public static double[] thirdOctaves = new double[] {20, 25, 31.5, 40, 50, 63, 80, 100, 125, 160, 200, 250, 315, 400, 500, 630, 800, 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300, 8000, 10000, 12500, 16000, 20000 };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fupin_result);
        mContext = this;

        findViewById(R.id.backImg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        lineChart = findViewById(R.id.lineChart);

        deviceList = FuPinFragment.deviceList;




        List<Float> avgList = new ArrayList<>();
        if (deviceList != null){
            for (int i = 0; i < 30; i++){
                float tmpAvg = 0;
                float tmpTotal = 0;
                Set<String> keySet = deviceList.keySet();
                for (String key : keySet) {
                    short[] tmpData = deviceList.get(key);
                    if (tmpData != null && i < tmpData.length){

                    }
                    tmpTotal = tmpTotal + tmpData[i];
                }
                tmpAvg = tmpTotal / keySet.size();
                avgList.add(tmpAvg);
            }
        }

        setData2(lineChart, avgList);
    }



    private void setData2(LineChart lineChart, List<Float> datas) {
        if (datas == null) {
            return;
        }

        ArrayList<String> xVals = new ArrayList<String>();
        for (int i = 0; i < thirdOctaves.length; i++) {
            xVals.add((thirdOctaves[i]  ) + "");
        }

        ArrayList<Entry> yVals1 = new ArrayList<Entry>();

        for (int i = 0; i < datas.size(); i++) {

            float val = (Float) (datas.get(i));// + (float)
            // ((mult *
            // 0.1) / 10);
            yVals1.add(new Entry(val, i));
        }


        LineDataSet set1;

        if (lineChart.getData() != null &&
                lineChart.getData().getDataSetCount() > 0) {
            set1 = (LineDataSet) lineChart.getData().getDataSetByIndex(0);
            set1.setYVals(yVals1);
            lineChart.getData().setXVals(xVals);
            lineChart.getData().notifyDataChanged();
            lineChart.notifyDataSetChanged();
        } else {
            // create a dataset and give it a type
            set1 = new LineDataSet(yVals1, "幅频特性");
            set1.setAxisDependency(YAxis.AxisDependency.LEFT);
            set1.setColor(ColorTemplate.getHoloBlue());
//            set1.setCircleColor(Color.WHITE);
            set1.setLineWidth(2f);
//            set1.setCircleRadius(3f);
            set1.setFillAlpha(65);
            set1.setFillColor(ColorTemplate.getHoloBlue());
            set1.setHighLightColor(Color.rgb(244, 117, 117));
            set1.setDrawCircleHole(false);
            //set1.setFillFormatter(new MyFillFormatter(0f));
            //set1.setDrawHorizontalHighlightIndicator(false);
            //set1.setVisible(false);
            //set1.setCircleHoleColor(Color.WHITE);


            ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
            dataSets.add(set1); // add the datasets

            // create a data object with the datasets
            LineData data = new LineData(xVals, dataSets);
            data.setValueTextColor(Color.WHITE);
            data.setValueTextSize(9f);

            // set data
            lineChart.setData(data);
            lineChart.setTouchEnabled(false);
            lineChart.setScaleEnabled(false);
            lineChart.setDragEnabled(false);
            lineChart.setScaleXEnabled(false);
            lineChart.setScaleYEnabled(false);
        }
    }

    /**
     * 计算多点倍频平均
     */
    private void  FuPinAvg(){
//        int noSpectralLines3 = 30;
//        if (splList == null || splList.size() == 0){
//            return;
//        }
//        List<Double> pinLvAvgList = new ArrayList<>();
//        for (int k = 0; k < noSpectralLines3; k++){
//            double tmpSum = 0;
//            for (int i = 0; i < splList.size(); i++){
//                tmpSum = tmpSum + splList.get(i).getData()[k];
//            }
//            pinLvAvgList.set(k, tmpSum / splList.size());
//        }
    }

}
